package com.xc.freeapp.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.xc.freeapp.exception.BaseException;

public class CheckUtils {
	
	/**
	 * 正则表达式验证参数
	 * @param code 
	 * @param obj //String obj="ceponline@yahoo.com.cn";
	 * @param patt //String patt="[//w//.//-]+@([//w//-]+//.)+[//w//-]+";
	 *  手机号验证：patt= "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
	 *  邮箱验证：patt="[//w//.//-]+@([//w//-]+//.)+[//w//-]+";
	 * */
    public static void matchPattern(String code,String obj,String patt)  throws BaseException{
    	
    	Pattern pattern = Pattern.compile(patt,Pattern.CASE_INSENSITIVE);
    	Matcher matcher = pattern.matcher(obj);
    	if(!matcher.matches()){
    		BaseException.throwBaseException(code);
    	}
    }
	
	/**
	 * 判断是否为null
	 * @param code 
	 * @throws BaseException 
	 */
	public static void notNull(String code,Object... targets) throws BaseException{
		if(targets==null)
			BaseException.throwBaseException(code);
		for (Object t : targets) {
			if(t==null)
				BaseException.throwBaseException(code);
		}
	}
	
	/**
	 * 判断是否为空 empty null 
	 * @param code 
	 * @param params "name"
	 * @throws BaseException 
	 */
	public static void notBlankByName(String code,String target,Object... params)throws BaseException{
		if(target==null||target.isEmpty())
			BaseException.throwBaseExceptionByName(code, params);
	}
	
	public static void main(String[] args) {
		try {
			CheckUtils.matchPattern("905", "13,2341", "\\d*,*\\d*");
		} catch (BaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
